#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h> 
#include <avr/portpins.h>

#include "XmegaSerial.h"
#include "XmegaADC.h"

#ifndef F_CPU
#define F_CPU 2000000UL
#endif

#include <util/delay.h>

//void Config32MHzClock(void);
void clck();
 
int main (void)
{
  char buffer[20];

	//clck();
	//Config32MHzClock();
	
 uart_init(9600);
 init_adc();


 PORTB.DIRSET = (1 << PIN0);
 
  while (1)
	{
		
    PORTB.OUT = (1 << PIN0);
    _delay_ms(150);
		
    int temp = readADC();

    itoa(temp, buffer, 10);
    //uart_putc('h');
    //uart_putc('e');
    //uart_putc('\n');
		
    
    //uart_puts(buffer);
	  
	  PORTB.OUT = (0 << PIN0);
    _delay_ms(5000);
	}		
}
void Config32MHzClock(void)
{
  CCP = CCP_IOREG_gc; //Security Signature to modify clock 
  // initialize clock source to be 32MHz internal oscillator (no PLL)
  OSC.CTRL = OSC_RC32MEN_bm; // enable internal 32MHz oscillator
  while(!(OSC.STATUS & OSC_RC32MRDY_bm)); // wait for oscillator ready
  CCP = CCP_IOREG_gc; //Security Signature to modify clock 
  CLK.CTRL = 0x01; //select sysclock 32MHz osc
};

void clck()
{
  //CCP = CCP_IOREG_gc; //Security Signature to modify clock 
	
	   /* Set to 32MHz Oscillator PIN1 = OSC_RC32MEN is set */
	    OSC.CTRL |= 0x02; 
		/* If Oscillator is stable, set the flag RC32MRDY * 32MHz and can be used */
		 while(!(OSC.STATUS & OSC_RC32MRDY_bm));
		 /* I / O Protection */
		  CCP = 0xd8, 
		 /* set clock to 32MHz */
		  CLK.CTRL = 0x01;

}
